package test.spider.jd;

import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jodd.http.HttpBrowser;
import jodd.http.HttpRequest;
import test.model.Goods;
import test.spider.jd.util.DBUtil;

public class App {
	
	public static ExecutorService es = Executors.newFixedThreadPool(8);
	public static String catids = "1315,1342,12089";
	public static String catname = "男士羊毛衫";

	public static void main(String[] args) {
//		System.out.println(time2Min(4546810l));
		
		//9987,653,655 手机
		//670,671,672 笔记本
		//1318,12099,9756跑步鞋
		//11729,11730,6908休闲鞋
		//1315,1343,9706毛呢大衣
		//1315,1343,9719连衣裙
		//1315,1343,1356女士针织衫
		//1315,1343,11986旗袍
		//1315,1343,11999 羊毛衫
		//1315,1343,9708女士风衣
		//1315,1343,1354女式衬衫
		//1315,1343,9713雪纺衫
		//1315,1343,1355女士T恤
		//1315,1342,1349男士T恤
		//1315,1342,9735男士牛仔裤
		//1315,1342,12089男士羊毛衫
		
		spiderJD();
	}
	
	static void spiderJD() {
		
		DBUtil.init();
		long l1 = System.currentTimeMillis();
		for(int i=1;i<=200;i++) {
			try {
				System.err.println("第"+i+"页");
				String url = "http://list.jd.com/list.html?cat="+App.catids+"&page="+i;
				
				HttpRequest request = HttpRequest.get(url);
				
				HttpBrowser browser = new HttpBrowser();
				browser.sendRequest(request);
				
//			HttpResponse response = request.send();
//			String html = response.bodyText();
				String html = browser.getPage();
				Document doc = Jsoup.parse(html);
				Elements elements = doc.select(".gl-item");
//				List<Goods> list = new ArrayList<>();
				elements.forEach(new Consumer<Element>() {
					@Override
					public void accept(Element t) {
						es.execute(new SpiderRunnable(t));
					}
				});
//				String sql = "insert into goods (name, company, jd_price, market_price, evaluate_count, brand, category) values (?, ?, ?, ?, ?, ?, ?)";
//				String columns = "name,company,jd_price,market_price,evaluate_count,brand,category";
//				int[] rows = Db.batch(sql, columns, list, 15);
//				for(int j=0;j<rows.length;j++) {
//					System.out.println(rows[j]);
//				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		es.shutdown();
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				long l2 = System.currentTimeMillis();
				System.err.println("共耗时："+time2Min(l2-l1));
			}
		});
	}
	
	
	static String time2Min(long ms) {
		long s = ms/1000;
		ms = ms%1000;
		long min = s/60;
		s = s%60;
		return min+"min"+s+"s"+ms+"ms";
	}
	
	

}


class SpiderRunnable implements Runnable {
	
	private Element t;
	
	public SpiderRunnable(Element t) {
		this.t = t;
	}

	@Override
	public void run() {
		try {
			String name = t.select(".p-name").select("a").select("em").text();
			String company = t.select(".p-shop").attr("data-shop_name");
			if(StringUtils.isEmpty(company)) company = "京东";
			String id = t.select(".p-focus").select("a").attr("data-sku");
			if(StringUtils.isEmpty(id)) {
				id = t.select(".p-operate").select("a").eq(1).attr("data-sku");
			}
			
			String priceUrl = "http://p.3.cn/prices/mgets?my=list_price&skuIds=J_"+id;
			String priceResult = HttpRequest.get(priceUrl).send().bodyText();
			JSONObject json = JSON.parseArray(priceResult).getJSONObject(0);
			BigDecimal jdprice = json.getBigDecimal("p");
			BigDecimal mprice = json.getBigDecimal("m");
			
			int evaluateCount = Integer.parseInt(t.select(".p-commit").select("a").text());
			
			String detailUrl = t.select(".p-img").select("a").attr("href");
			String result = HttpRequest.get("http:"+detailUrl).send().bodyText();
			Document detailDoc = Jsoup.parse(result);
			String brand = detailDoc.select(".breadcrumb").select("span").eq(1).select("a").eq(0).text();
			
			Goods goods = new Goods();
			goods.set("name", name);
			goods.set("company", company);
			goods.set("jd_price", jdprice);
			goods.set("market_price", mprice);
			goods.set("evaluate_count", evaluateCount);
			goods.set("brand", brand);
			
			goods.set("category", App.catname);
			
//			list.add(goods);
			
			goods.save();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

